import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * JDBC实现登录功能[DQL]
 * 1.用户名和密码正确,登录成功
 * 2.用户名或密码错误
 * SQL语句: SELECT username,password,nickname,age FROM userinfo WHERE username='' AND password=''
 */
public class JDBCLogin {
    public static void main(String[] args) {
        //接收用户输入
        Scanner scanner = new Scanner(System.in);
        System.out.println("登录功能");
        System.out.println("请输入用户名:");
        String username = scanner.nextLine();
        System.out.println("请输入密码:");
        String password = scanner.nextLine();
        // 数据库操作[注意:自动关闭特性]
        try (Connection connection = DBUtil.getConnection();){
            Statement statement = connection.createStatement();
            // "+变量名+"
            String sql = "SELECT username,password,nickname,age FROM tedu.userinfo WHERE username='"+username+"' AND password='"+password+"'";
            // 注意方法:executeQuery("SQL语句")
            ResultSet resultSet = statement.executeQuery(sql);
            /*
                1.登录成功: 欢迎你,昵称
                2.登录失败: 用户名或密码错误
             */
            if (resultSet.next()){
                //登录成功
                String nickname = resultSet.getString("nickname");
                System.out.println("欢迎你," + nickname);
            }else {
                System.out.println("用户名或密码错误");
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}










